﻿using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using InfoJetSoft.Service.Entity;

namespace InfoJetSoft.Service.Util
{

	class SchemaUtil
	{
		
		public static bool canRemoveAttr(Form form, XmlElement parentElement, XmlAttribute attr)
		{
			return true;
		}
		
		public static bool canSetAttr(Form form, XmlElement parentElement, XmlAttribute attr)
		{
			return true;
		}
		
		public static bool canRemoveChild(Form form, XmlElement parentElement, XmlElement childElement)
		{
			if( !childElement.ParentNode.Equals( parentElement ) ){
				return true;
			}
			XmlNode siblingNode = childElement.NextSibling;
			parentElement.RemoveChild( childElement );
			try{
				SchemaValidation validation = new SchemaValidation( form );
				validation.Validate();
				return validation.IsValid( parentElement );
			}catch( Exception e ){
				return false;
			}finally{
				parentElement.InsertBefore( childElement, siblingNode );
			}
		}
		
		public static bool canAppendChild(Form form, XmlElement parentElement, XmlElement childElement) 
		{
			parentElement.AppendChild( childElement );
			try{
				SchemaValidation validation = new SchemaValidation( form );
				validation.Validate();
				return validation.IsValid( parentElement );
			}catch( Exception e ){
				return false;
			}finally{
				parentElement.RemoveChild( childElement );
			}
		}
		
		public static bool canInsertBefore(Form form, XmlElement parentElement, XmlElement beforeElement, XmlElement contextElement)
		{
			parentElement.InsertBefore( beforeElement , contextElement );
			try{
				SchemaValidation validation = new SchemaValidation( form );
				validation.Validate();
				return validation.IsValid( parentElement );
			}catch( Exception e ){
				return false;
			}finally{
				parentElement.RemoveChild( beforeElement );
			}
		}

		public static bool canInsertAfter(Form form, XmlElement parentElement, XmlElement afterElement, XmlElement contextElement) {
			parentElement.InsertAfter( afterElement , contextElement );
			try{
				SchemaValidation validation = new SchemaValidation( form );
				validation.Validate();
				return validation.IsValid( parentElement );
			}catch( Exception e ){
				return false;
			}finally{
				parentElement.RemoveChild( afterElement );
			}
		}
		
		
		public static XmlElement getNextSiblingElement(XmlNode itemNode)
		{
			itemNode = itemNode.NextSibling;
			while (itemNode != null)
			{
				if (itemNode is XmlElement)
				{
					return (XmlElement) itemNode;
				}
				else
				{
					itemNode = itemNode.NextSibling;
				}
			}
			return null;
		}
		
		public static int getChildElementCount(XmlElement parentElement)
		{
			int count = 0;
			XmlNodeList childXmlNodeList = parentElement.ChildNodes;
			foreach (XmlNode node in childXmlNodeList)
			{
				if (node is XmlElement)
				{
					count++;
				}
			}
			return count;
		}

		//CID 0384 //2009.08.12 //Begin
		public static bool isXhtmlDocument( string xhtml )
		{
            //CID 1333 //2012.09.16 //Begin
            if (!xhtml.StartsWith("<"))
            {
                throw new InfoJetException("not xml");
            }
            //CID 1333 //2012.09.16 //End
			StringReader strReader = new StringReader( xhtml );
			XmlTextReader reader = new XmlTextReader( strReader );
			bool isXhtml = true;
			while( reader.Read() )
			{
				if( reader.NodeType == XmlNodeType.Element )
				{
					if( !( reader.NamespaceURI != null && reader.NamespaceURI.Equals( "http://www.w3.org/1999/xhtml" ) ) )
					{
                        //CID 1333 //2012.09.16 //Begin
                        if (!xhtml.StartsWith("<"))
                        {
                            reader.Close();
                            throw new InfoJetException("not xml");
                        }
                        //CID 1333 //2012.09.16 //End
					}
				}
			}
			reader.Close();
			return isXhtml;
		}
		//CID 0384 //2009.08.12 //End

		//CID 0384 //2009.08.12 //Begin
		//判断DocumentElement中的内容是否是Xhtml.
		public static bool isXhtmlContent( string xhtmlContent )
		{
			string xhtml = "<div xmlns=\"http://www.w3.org/1999/xhtml\">" + xhtmlContent + "</div>";
			return isXhtmlDocument( xhtml );
		}
		//CID 0384 //2009.08.12 //End
	}
}